package com.multipleThread.myRestaurant;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 罗俊华
 * @date 2021/4/29 - 3:31 下午
 */
//接待员
@Data
public class Receptionist implements Runnable {

    private ExecutorService executorService;

    private RestaurantPlus restaurantPlus;

    private static final Random RANDOM = new Random();

    private static final AtomicInteger count = new AtomicInteger();

    //    接待员id
    private int recptionistId = count.getAndIncrement();

    public Receptionist(ExecutorService executorService, RestaurantPlus restaurantPlus) {
        this.executorService = executorService;
        this.restaurantPlus = restaurantPlus;
    }

    @Override
    public void run() {
        while (!Thread.interrupted()) {
//        一张桌子上客户的数量
            int customerCountOfOneTable = RANDOM.nextInt(12);

            Table table = new Table();

            OrderTicket orderTicket = new OrderTicket(table);
//      将该桌的票据存储饭店
            this.restaurantPlus.getOrderTickets().put(table, orderTicket);

//        List<Customer> customers = new ArrayList<>();

            for (int i = 0; i < customerCountOfOneTable; i++) {
                Customer customer = new Customer(table, this.restaurantPlus);
//            table.addCustomer(customer); 加上就死锁
                executorService.execute(customer);
            }

            System.out.printf("接待员【%d】招揽到一桌【%d】人的客户群\n", recptionistId, customerCountOfOneTable);

//        table.addCustomers(customers);
        }
    }
}
